package com.mcxx.modules.common.service.impl;

import com.mcxx.modules.common.service.StatisticService;
import com.mcxx.modules.statistics.dto.StatisticDTO;
import com.mcxx.modules.statistics.dto.SubStatisticsDTO;
import com.mcxx.modules.statistics.param.StatisticParam;
import com.mcxx.modules.statistics.service.read.StatisticReadService;
import com.mcxx.modules.system.entity.AreaSalvationEntity;
import com.mcxx.modules.system.service.read.SalvationRuleReadService;
import com.mcxx.util.*;
import com.yinhai.ta404.component.org.orguser.area.entity.TaAreaPo;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;



@Slf4j
@Service
@TaTransactional
public class StatisticSubServiceImpl extends StatisticService {
    @Autowired
    private AreaUtil areaUtil;
    @Autowired
    private StatisticReadService statisticReadService;
    @Autowired
    private SalvationRuleReadService salvationRuleReadService;

    @Override
    public List<? extends StatisticDTO> getStatistic(StatisticParam param, UserAccountVo accountVo, String standType) {
        List<TaAreaPo> taAreaPos = new ArrayList<TaAreaPo>();
        List<Integer> levels = new ArrayList<Integer>();
        String name = super.commonStatisticStart(param, accountVo, taAreaPos, levels);

        List<String> standards = new ArrayList<String>();
        standards.add(standType);

        int precision = 2;
        if (!taAreaPos.get(0).getAreaLevel().equals(Constant.AreaLevel.PROVINCE)){
            precision = 4;
        }
        List<SubStatisticsDTO> results = null;
        if(standType.equals(Constant.BusinessStandType.CITY_DIBAO)){
            name += "城市低保综合统计报表";
            results = statisticReadService.getCitySubsistenceComp(
                    areaUtil.getValidCode(taAreaPos.get(0).getAreaCode(), taAreaPos.get(0).getAreaLevel()), levels,
                    param.getMonth(), precision);
        }
        else{
            name += "农村低保综合统计报表";
            results = statisticReadService.getCountrySubsistenceComp(
                    areaUtil.getValidCode(taAreaPos.get(0).getAreaCode(), taAreaPos.get(0).getAreaLevel()), levels,
                    param.getMonth(), precision);
        }
        super.commonStatisticEnd(name, standards, taAreaPos, levels, results);
        List<SubStatisticsDTO> lastResults = results.stream().filter(e -> !e.getAreaCode().endsWith("99900")
                && !e.getAreaName().startsWith("测试") && !e.getAreaName().endsWith("省本级合计")).collect(Collectors.toList());
        return lastResults;
    }

    @Override
    public void fillSalvation(StatisticDTO dto, List<String> standards, List<AreaSalvationEntity> salivations) {
        ((SubStatisticsDTO)dto).setStandard(salivations.get(0).getStandardMoney());
    }
}
